
// Standard elements tracked
dmsg("Begin standard components...");
var commentParse = new Object();
var ENUM=new Object();
var beta=false; // allow use of Beta features, i.e. all Geogebra run GGB 5 Beta libraries
var develop=false; // add support materials for site development

// Load common libraries
/* dmsg("import libraries...");

var holdCount = 0;
holdCount++; $.getScript("./js/standardEnumeration.js", function(data,status,jqxhr){ $.holdReady( holdCount-- > 0 ); dmsg("Enumeration " + status);}); // Coding ENUMERATION
holdCount++; $.getScript("./js/webColor.js", function(data,status,jqxhr){ $.holdReady( holdCount-- > 0 ); dmsg("Color " + status);}); // Color coding enumeration
holdCount++; $.getScript("./js/geogebraWrapper.js",function(data,status,jqxhr){ $.holdCount( holdCount-- > 0 ); dmsg("Geogebra " + status);}); // Geogebra javascript interface
//holdCount++; $.getScript("./js/googleTranslateWrapper.js",dmsg("Google Translate")); // Google Translation Service
holdCount++; $.getScript("./js/quizTools.js",function(data,status,jqxhr){ $.holdCount( holdCount-- > 0 ); dmsg("Quiz " + status);}); // functions to help analyse response from user
holdCount++; $.getScript("./js/en/keywords.js",function(data,status,jqxhr){ $.holdCount( holdCount-- > 0 ); dmsg("Keywords " + status);}); // Language localisation for javascript
holdCount++; $.getScript("http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML",function(data,status,jqxhr){ $.holdCount( holdCount-- > 0 ); dmsg("MathJax " + status);}); // Latex parser for math formulae
 */

// Cookie/URL handlers W3C code
dmsg("Cookie handlers...");
var urlparams = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++){
	hash = hashes[i].split('=');
	urlparams.push(hash[0]);
	urlparams[hash[0]] = hash[1];
	dmsg( "URL(" + hash[0] + "=" + hash[1] + ")" );
}

var stylesheet = "./css/standard.css";
dmsg( "URL:css " + typeof (urlparams["css"]) );
if ( typeof (urlparams["css"]) == "string" ) {
	stylesheet = urlparams["css"];
}
document.write("<link rel=\"stylesheet\" href=\"" + stylesheet + "\" type=\"text/css\"></style>");
dmsg( "css " + stylesheet );

if ( typeof (urlparams["beta"]) == "string" ) {
	if ( urlparams["beta"] == "true" ) {
		beta = true;
	}
}

if ( typeof (urlparams["develop"]) == "string" ) {
	if ( urlparams["develop"] == "true" ) {
		develop = true;
	}
}

function revealBlock( command, attributes ) {
	dmsg("reveal the blocks " + command + "," + attributes);
	var idList = attributes.split(/[ {}(),]/g); // get the list of id's - Q&D
	dmsg( idList );
	for ( var i=0, len=idList.length; i < len; i++){
		if ( idList[i].length > 0 ) { // only process named identities
			dmsg( i + "/" + len + ":'" + idList[i] + "'" );
			$("#" + idList[i]).show(); // reveal all id coded
			$("." + idList[i]).show(); // reveal all class coded
		}
	}
}

commentParse["reveal"] = revealBlock;

// create index of the current page
function indexPage() {
	var current;	// the currently parsed html element - similar to 'this'.

	// Table of content place holder h1,h2,h3
	var elmSection = $("#toc").append('<div id="_section"></div>');
	
	// References place holder cite
	var elmReference = $("#toc").append('<div id="_reference"></div>');
	
	// Defintions place holders dl
	var elmGlossary = $("#toc").append('<div id="_glossary"></div>');
	
	var linkRef = ""; // id attribute of html
	var section = 0;	// number of sections found
	var reference = 0;	// number of references found
	var glossary = 0;	// number of glossary items found
	var indexLevel = "";	// string indenting sections
	
	// Find the key section headings
	$("h1, h2, h3").each(function(i) {
		current = $(this);
		linkRef = current.attr("id");

		// set the indentation based upon the heading style
		if ( current.is("h1" ) ) {
			indexLevel = "&loz;&nbsp;";
		} else if ( current.is("h2" ) ) {
			indexLevel = "&nbsp;&nbsp;&diams;&nbsp;";
		} else if ( current.is("h3" ) ) {
			indexLevel = "&nbsp;&nbsp;&nbsp;&nbsp;&bull;&nbsp;";
		}
		
		// increment section count - if first section display TOC heading
		section += 1;
		if ( section == 1 ) {
			elmSection.append('<strong>' + ENUM["section"] + '</strong><br />');
		}
		
		// create a link id if necessary
		if ( (linkRef == null) && (linkRef == "") ) {
			linkRef = "_link" + section;
			current.attr("id", linkRef);
		}

		// add the title to the toc
		elmSection.append(indexLevel + "<a href='#" + linkRef + "'>" + current.html() + "</a><br />");
		$(this).append($("<a>", { "id": linkRef }));
	});

	// find embedded definitions and add them to the dictionary list (dl)/glossary
	if ($("dl")) {
		$("dfn").each(function(i) {
			current = $(this);
			var word = current.attr("id");
			if (( word != null ) && ( word != "" )){
				var defn = current.html();
				$("dl").append("<dt>" + word + "</dt><dd>" + defn + "</dd>");
			}
		});
	}

	// glossary items
	$("dt").each(function(i) {
		current = $(this);
		linkRef = current.attr("id");
		
		var word;
		var bodyTextStart;
		var bodyTextEnd;
		
		// increment glossary count, if first put the TOC heading
		glossary += 1;
		if ( glossary == 1 ){
			elmGlossary.append('<strong>' + ENUM["glossary"] + '</strong><br />');
		}
		
		// html code for special marking - i.e. dfn tag used to embed the definition in the body text
		bodyTextStart = "";
		bodyTextEnd = "";
		
		if ( (linkRef == null) || (linkRef == "") ) {
			linkRef = "_link" + section;
			current.attr("id", linkRef);
		}
		word = current.text();
		
		// add the glossary item to the TOC
		if ( (linkRef != null) && (linkRef != "") ) {
			elmGlossary.append(bodyTextStart + "<a href='#" + linkRef + "'>" + word + "</a>" + bodyTextEnd + "<br />");
		}
	});
}
dmsg("created index function");

/** Allow the comment parser to be used as a standard unit, callable at will, i.e. as part of init for the original document; will be coded */
function parseComments( rootElement ){
	dmsg("Parse Comments " + typeof(rootElement));
	$(rootElement).hide();
	$(rootElement).contents().each( function(){
		dmsg("Find children " + this.nodeType);
		if ( this.nodeType == DOM_COMMENT ) { // comments only
			var lComment = this.nodeValue;
				
			// trim extra space
			lComment = lComment.replace(/^\s+|\s+$/g, "");
				
			// first element is the object id that is being referred to
			var embedInstruction = lComment.split(" ");

			// use the relevant callback routine to parse these commands
			if ( commentParse[embedInstruction[0]] ) {
				dmsg("Pass comment to parser " + embedInstruction[0] + "," + embedInstruction[1]);
				jComments = commentParse[embedInstruction[0]](embedInstruction[0],embedInstruction[1],this);
			} else {
				// unprocessed comment put into the comment stack
				dmsg("//" + this.nodeValue );
			}
		}
	});
}


// Initialise the jQuery automatic styling
function init(){
	// all objects set to be (class) "reveal" then hide is first
	dmsg( "hide all reveals" );
	$("*.reveal").hide(dmsg("hide complete") );

	// when an object with a "show" class is clicked, open the relevant "reveal" class
	dmsg( "create show operations" );
	$("*.show").click( function() { parseComments(this); } );
}

dmsg("standardComponets.js Done.");

/**
 * Library:		standardComponents.js
 *
 * Description:	standard components for the development of web pages
 *
 * Content:
 *	autoindex		[]		Page Indexing functions, marks H1-H3 in a Table of Content "TOC"
 *  init					Creates various functions using jQuery:
 *    show and reveal sections; any element with a class "show" and id value, uses fade in to reveal the "reveal" classes with the same id
 *    as code is revealled html comments are parsed for embedded commands; i.e. geogebra applet control <target>:<command>
 *
 * History:
 *  Version		Date		Author		Description
 *	0.01		2011-03-28	PBoothroyd	Library begun to allow automatic creation of standard elements for webpages.
 *  0.02		2013-01-17	PBoothroyd	Major amendments made to ensure jQuery functionality (now 1.8) added URL parameterisation and stylesheet modifier code
 */
 